package com.ocean.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocean.admin.core.annotation.Log;
import com.ocean.admin.core.annotation.enums.BusinessType;
import com.ocean.admin.extension.security.core.SysUserDetails;
import com.ocean.admin.extension.security.filter.WebFilterExtend;
import com.ocean.admin.model.dto.user.SysUserQueryDTO;
import com.ocean.admin.model.dto.user.SysUserSaveDTO;
import com.ocean.admin.model.dto.user.SysUserUpdateDTO;
import com.ocean.admin.model.dto.user.SysUserUpdatePasswordDTO;
import com.ocean.admin.model.vo.router.RouterVo;
import com.ocean.admin.model.vo.user.SysUserInfoVo;
import com.ocean.admin.model.vo.user.SysUserVo;
import com.ocean.admin.service.ISysUserService;
import com.ocean.core.util.response.ResultSet;
import com.ocean.security.model.UserLoginDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/admin/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 管理员登陆
     * 只做暴露接口作用
     *
     * @param loginDTO 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public ResultSet<String> login(@Validated @RequestBody UserLoginDTO loginDTO) {
        return null;
    }

    /**
     * 获取用户信息
     *
     * @return 用户
     */
    @GetMapping("/userInfo")
    @Operation(summary = "获取登录用户信息")
    public ResultSet<SysUserDetails> userInfo() {
        return ResultSet.ok(WebFilterExtend.current());
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @Operation(summary = "获取路由信息")
    @GetMapping(value = "getRouter")
    public ResultSet<List<RouterVo>> getRouters() {
        return ResultSet.ok(iSysUserService.getRouters());
    }

    /**
     * 新增用户
     *
     * @param saveDTO 用户信息
     * @return 结果
     */
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermission('system:user:saveUser')")
    @Operation(summary = "新增用户")
    @PostMapping(value = "saveUser")
    public ResultSet<String> saveUser(@Validated @RequestBody SysUserSaveDTO saveDTO) {
        return ResultSet.ok(iSysUserService.saveUser(saveDTO));
    }

    /**
     * 删除用户
     *
     * @param userIds 用户ID
     * @return 结果
     */
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermission('system:user:removeUser')")
    @Operation(summary = "删除用户")
    @GetMapping(value = "removeUser/{userIds}")
    public ResultSet<String> removeUser(@PathVariable Long[] userIds) {
        return ResultSet.ok(iSysUserService.removeUser(userIds));
    }

    /**
     * 修改用户
     *
     * @param updateDTO 用户信息
     * @return 结果
     */
    @Operation(summary = "修改用户")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermission('system:user:updateUser')")
    @PostMapping(value = "updateUser")
    public ResultSet<String> updateUser(@Validated @RequestBody SysUserUpdateDTO updateDTO) {
        return ResultSet.ok(iSysUserService.updateUser(updateDTO));
    }

    /**
     * 分页查询用户
     *
     * @param queryDTO 查询条件
     * @return 结果
     */
    @Operation(summary = "分页查询用户")
    @PreAuthorize("@ss.hasPermission('system:user:pageList')")
    @PostMapping(value = "pageList")
    public ResultSet<IPage<SysUserVo>> pageList(@Validated @RequestBody SysUserQueryDTO queryDTO) {
        return ResultSet.ok(iSysUserService.pageList(queryDTO));
    }

    /**
     * 查询用户详情
     *
     * @param userId 用户ID
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermission('system:user:pageList')")
    @Operation(summary = "查询用户详情")
    @GetMapping(value = "queryInfo/{userId}")
    public ResultSet<SysUserInfoVo> queryInfo(@PathVariable Long userId) {
        return ResultSet.ok(iSysUserService.queryInfo(userId));
    }

    /**
     * 重置密码
     *
     * @param passwordDTO 密码信息
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermission('system:user:updateUser')")
    @Operation(summary = "重置密码")
    @PostMapping(value = "resetPassword")
    public ResultSet<String> resetPassword(@Validated @RequestBody SysUserUpdatePasswordDTO passwordDTO) {
        return ResultSet.ok(iSysUserService.resetPassword(passwordDTO));
    }

    /**
     * 修改用户状态
     *
     * @param userId 用户ID
     * @param status 帐号状态（0正常 1停用）
     * @return 结果
     */
    @Operation(summary = "修改用户状态")
    @PreAuthorize("@ss.hasPermission('system:user:updateUser')")
    @GetMapping(value = "updateStatus/{userId}/{status}")
    public ResultSet<String> updateStatus(@PathVariable Long userId, @PathVariable Integer status) {
        return ResultSet.ok(iSysUserService.updateStatus(userId, status));
    }
}
